<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class HintStatus extends Model
{
    protected $fillable = [
        'class',
        'hintable_id',
        'hintable_type',
        'comment', // 备注
    ];

    protected $hidden = [
        'comment',
        'created_at',
        'updated_at',
    ];

    protected $table = 'hint_statuses';

    // 订单是否完成
    protected $isFinish = false;

    // 订单是否允许评论
    protected $canComment = false;

    // 订单是否在运输中
    protected $isShip = false;

    const STATUS_CODE = [
        // 订单取消
        CancelHintStatus::class => 0,
        // 未付款: 订单初始状态, 默认的状态
        UnPayHintStatus::class => 1,
        // 线下转账-待转账
        OfflineUnPayHintStatus::class => 2,
        // 线下转账-待确认
        PayingHintStatus::class => 3,
        // 付款成功/转账成功: 未发货状态
        UnShipHintStatus::class => 4,
        // 发货成功: 待收货状态
        UnReceivedHintStatus::class => 5,
        // 收货成功: 待评论状态
        UnCommentHintStatus::class => 6,
        // 评论完成/自动评论: 订单完成
        DoneHintStatus::class => 7,
        // 退换货初始状态
        RefundAppliedHintStatus::class => 10,
        // 拒绝退款
        RefundFailureHintStatus::class => 11,
        // 同意退款
        RefundSuccessHintStatus::class => 12,
        // 退款完成
        RefundDoneHintStatus::class => 13,
        // 退款取消
        RefundCancelHintStatus::class => 14,
    ];

    // 获取状态对象
    public function status(): HintStatus
    {
        $model = new $this->class();

        $ret = $model->newFromBuilder($this->attributes);

        if (!($ret instanceof HintStatus)) {
            throw new \Exception("StatusHints TypeError");
        }

        $ret->setAttribute($this->getKeyName(), $this->getKey());

        return $ret;
    }

    // 获取状态描述
    public function toString(): string
    {
        return $this->desc;
    }

    // 订单是否完成
    public function isFinish(): bool
    {
        return $this->isFinish;
    }

    // 是否允许评论
    public function canComment(): bool
    {
        return $this->canComment;
    }

    // 是否运输中
    public function isShip()
    {
        return $this->isShip;
    }

    // 获取关联的模型
    public function hintable()
    {
        return $this->morphTo();
    }

    // 线上付款成功
    public function pay($comment = ''): HintStatus
    {
        return $this;
    }

    // 选择线下付款
    public function offlinePayment($comment = ''): HintStatus
    {
        return $this;
    }

    // 线下付款信息填写完成
    public function offlinePaid($comment = ''): HintStatus
    {
        return $this;
    }

    // 发货
    public function ship($comment = ''): HintStatus
    {
        return $this;
    }

    // 收货
    public function receive($comment = ''): HintStatus
    {
        return $this;
    }

    // 评价
    public function comment($comment = ''): HintStatus
    {
        return $this;
    }

    // 退款
    public function refund($comment = ''): HintStatus
    {
        return $this;
    }

    // 同意退款
    public function access($comment = ''): HintStatus
    {
        return $this;
    }

    // 拒绝退款
    public function refuse($comment = ''): HintStatus
    {
        return $this;
    }

    // 取消订单
    public function cancel($comment = ''): HintStatus
    {
        return $this;
    }

    // 退款完成
    public function done($comment = ''): HintStatus
    {
        return $this;
    }
}

// 订单刚生成, 未付款状态, 即订单的初始状态
class UnPayHintStatus extends HintStatus
{
    protected $desc = "待付款";

    // 线上付款成功, 或者钱包支付成功
    public function pay($comment = ''): HintStatus
    {
        $this->class = UnShipHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }

    // 选择线下付款
    public function offlinePayment($comment = ''): HintStatus
    {
        $this->class = OfflineUnPayHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }

    // 取消订单
    public function cancel($comment = ''): HintStatus
    {
        $this->class = CancelHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 线下转账, 待转账
class OfflineUnPayHintStatus extends HintStatus
{
    protected $desc = "待转账";

    public function offlinePaid($comment = ''): HintStatus
    {
        $this->class = PayingHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }

    // 取消订单
    public function cancel($comment = ''): HintStatus
    {
        $this->class = CancelHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 线下转账, 待确认
class PayingHintStatus extends HintStatus
{
    protected $desc = "转账待确认";

    public function pay($comment = ''): HintStatus
    {
        $this->class = UnShipHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 已付款, 未发货状态
class UnShipHintStatus extends HintStatus
{
    protected $desc = "未发货";

    public function ship($comment = ''): HintStatus
    {
        $this->class = UnReceivedHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 已发货, 待收货状态
class UnReceivedHintStatus extends HintStatus
{
    protected $desc = "待收货";

    protected $isShip = true;

    // 收货
    public function receive($comment = ''): HintStatus
    {
        $this->class = UnCommentHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 收货完成, 待评价
class UnCommentHintStatus extends HintStatus
{
    protected $desc = "待评价";

    protected $canComment = true;

    // 评价
    public function comment($comment = ''): HintStatus
    {
        $this->class = DoneHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }

    // 申请退款
    public function refund($comment = ''): HintStatus
    {
        $this->class = RefundAppliedHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }

    // 订单完成
    public function done($comment = ''): HintStatus
    {
        $this->class = DoneHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 评价完成, 正常完成
class DoneHintStatus extends HintStatus
{
    protected $desc = "订单完成";

    protected $isFinish = true;

    // 申请退款
    public function refund($comment = ''): HintStatus
    {
        $this->class = RefundAppliedHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 申请退款, 还未同意状态
class RefundAppliedHintStatus extends HintStatus
{
    protected $desc = "已申请退款";
    protected $isRefunding = true;

    // 同意退款
    public function access($comment = ''): HintStatus
    {
        $this->class = RefundSuccessHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }

    // 拒绝退款
    public function refuse($comment = ''): HintStatus
    {
        $this->class = RefundFailureHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }

    // 取消退款
    public function cancel($comment = ''): HintStatus
    {
        $this->class = RefundCancelHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 申请退款, 拒绝
class RefundFailureHintStatus extends HintStatus
{
    protected $desc = "拒绝退款";
    protected $isRefunding = true;

    // 重新申请退款
    public function refund($comment = ''): HintStatus
    {
        $this->class = RefundAppliedHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 申请退款, 同意
class RefundSuccessHintStatus extends HintStatus
{
    protected $desc = "退款中";
    protected $isRefunding = true;

    // 退款完成
    public function done($comment = ''): HintStatus
    {
        $this->class = RefundDoneHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 退款成功
class RefundDoneHintStatus extends HintStatus
{
    protected $desc = "退款成功";
    protected $isRefunding = true;
}

// 退款取消
class RefundCancelHintStatus extends HintStatus
{
    protected $desc = "退款取消";
    protected $isRefunding = true;

    // 重新申请退款
    public function refund($comment = ''): HintStatus
    {
        $this->class = RefundAppliedHintStatus::class;
        $this->comment = $comment;
        $this->save();
        return $this;
    }
}

// 订单取消, 异常结束
class CancelHintStatus extends HintStatus
{
    protected $desc = "已取消";
    protected $isFinish = true;
}
